package edu.gatech.cs2340.thenullterminators.wheresmystuff;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
 * Controls the functionality of the
 * Edit Admin page.
 * 
 * @author theNullTerminators
 * @version 1.0
 *
 */
public class AdminEditActivity extends Activity {

	RadioGroup lGroup, aGroup;
	RadioButton rblock, rbunlock, rbadmin, rbnonadmin;
	EditText tUser, tPass, tPhon;
	String tempName, tempPass, tempPhon;
	Button bSub, bDel, bCan;

	Context context;
	CharSequence text = "Error!";
	int duration = Toast.LENGTH_SHORT;

	/**
	 * Sets up all the buttons and edit fields
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin_edit_user);
		tUser = (EditText) findViewById(R.id.aded_usr);
		tPass = (EditText) findViewById(R.id.aded_pass);
		tPhon = (EditText) findViewById(R.id.aded_phon);
		bSub = (Button) findViewById(R.id.aded_tosubmit);
		bDel = (Button) findViewById(R.id.aded_todelete);
		bCan = (Button) findViewById(R.id.aded_tocancel);
		lGroup = (RadioGroup) findViewById(R.id.aded_lockGroup);
		aGroup = (RadioGroup) findViewById(R.id.aded_adminGroup);
		rblock = (RadioButton) findViewById(R.id.aded_lock);
		rbunlock = (RadioButton) findViewById(R.id.aded_unlock);
		rbadmin = (RadioButton) findViewById(R.id.aded_admin);
		rbnonadmin = (RadioButton) findViewById(R.id.aded_nonadmin);
		context = getApplicationContext();

		// set text to users current
		tUser.setText(Login.users.get(AdminActivity.clickTask).getUserName());
		tPass.setText(Login.users.get(AdminActivity.clickTask).getPassWord());
		tPhon.setText(Login.users.get(AdminActivity.clickTask).getPhoneNumber());

		// set locked to users current
		if (Login.users.get(AdminActivity.clickTask).getIsLocked()) {
			rblock.setChecked(true);
		} else {
			rbunlock.setChecked(true);
		}

		// set admin to users current
		if (Login.users.get(AdminActivity.clickTask).getIsAdmin()) {
			rbadmin.setChecked(true);
		} else {
			rbnonadmin.setChecked(true);
		}

		bSub.setOnClickListener(new View.OnClickListener() {

			/**
			 * checks for valid information and informs the 
			 * user of errors, or edits the users data
			 */
			@Override
			public void onClick(View v) {
				tempName = tUser.getText().toString();
				tempPass = tPass.getText().toString();
				tempPhon = tPhon.getText().toString();
				if (rbnonadmin.isChecked()
						&& !Login.users.get(AdminActivity.clickTask)
								.getUserName().equals("admin")) {
					Login.users.get(AdminActivity.clickTask).setAdmin(false);
					int checkVal = Login.adminEditUser(tempName, tempPass,
							tempPhon, AdminActivity.clickTask);
					if (checkVal == -1) {
						text = "Username already exists";
						Toast.makeText(context, text, duration).show();
					} else if (checkVal == -2) {
						text = "Username should be a valid email address";
						Toast.makeText(context, text, duration).show();
					} else if (checkVal == -3) {
						text = "Username cannot be blank";
					} else if (checkVal == -4) {
						text = "Password must be at least 5 characters";
					} else if (checkVal == -5) {
						text = "Password cannot be blank";
					} else {
						text = "Reg user Edit successful!! " + checkVal;
						Toast.makeText(context, text, duration).show();
						if (rbunlock.isChecked()) {
							Login.users.get(AdminActivity.clickTask)
									.setIsLocked(false);
							Login.users.get(AdminActivity.clickTask)
									.resetFails();
						}
						if (rblock.isChecked()) {
							Login.users.get(AdminActivity.clickTask)
									.setIsLocked(true);
						}
						Intent myGoto = new Intent(v.getContext(),
								AdminActivity.class);
						v.getContext().startActivity(myGoto);
					}
					Toast.makeText(context, text, duration).show();
				} else if (rbadmin.isChecked()
						&& !Login.users.get(AdminActivity.clickTask)
								.getUserName().equals("admin")) {

					int checkVal = Login.adminEditAdmin(tempName, tempPass,
							AdminActivity.clickTask);
					if (checkVal == -1) {
						text = "Username already exists";
						Toast.makeText(context, text, duration).show();
					} else if (checkVal == -3) {
						text = "Username cannot be blank";
					} else if (checkVal == -4) {
						text = "Password must be at least 5 characters";
					} else if (checkVal == -5) {
						text = "Password cannot be blank";
					} else {
						text = "Admin user Edit successful!!";
						Toast.makeText(context, text, duration).show();
						Login.users.get(AdminActivity.clickTask).setAdmin(true);
						if (rbunlock.isChecked()) {
							Login.users.get(AdminActivity.clickTask)
									.setIsLocked(false);
							Login.users.get(AdminActivity.clickTask)
									.resetFails();
						}
						if (rblock.isChecked()) {
							Login.users.get(AdminActivity.clickTask)
									.setIsLocked(true);
						}
						Intent myGoto = new Intent(v.getContext(),
								AdminActivity.class);
						v.getContext().startActivity(myGoto);
					}
				} else if (Login.users.get(AdminActivity.clickTask)
						.getUserName().equals("admin")) {
					text = "Cannot Edit root admin";
				}

				Toast.makeText(context, text, duration).show();
			}
		});

		bDel.setOnClickListener(new View.OnClickListener() {

			/**
			 * deletes the users data and account
			 */
			@Override
			public void onClick(View v) {

				if (Login.users.get(AdminActivity.clickTask).getUserName()
						.equals(Login.currentUser.getUserName())) {
					text = "Can not delete yourself!";
					Intent myGoto = new Intent(v.getContext(),
							AdminActivity.class);
					v.getContext().startActivity(myGoto);
				} else {
					Login.users.remove(AdminActivity.clickTask);
					text = "Delete successful!!";
					Toast.makeText(context, text, duration).show();
					Intent myGoto = new Intent(v.getContext(),
							AdminActivity.class);
					v.getContext().startActivity(myGoto);
				}

				Toast.makeText(context, text, duration).show();
			}
		});

		bCan.setOnClickListener(new View.OnClickListener() {

			/**
			 * cancels all changes
			 */
			@Override
			public void onClick(View v) {

				text = "Cancel Edit!";
				Toast.makeText(context, text, duration).show();
				Intent myGoto = new Intent(v.getContext(), AdminActivity.class);
				v.getContext().startActivity(myGoto);

				Toast.makeText(context, text, duration).show();
			}
		});

	}

	/**
	 * sets up the menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.admin_edit_menu, menu);
		return true;
	}

	/**
	 * creates intent for admin profile page or 
	 * logout
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case R.id.admin_profile:
			Intent intent = new Intent(this,AdminActivity.class);
			this.startActivity(intent);
			break;
		case R.id.logOut:
			Login.logOut();
			Intent intent1 = new Intent(this,MainActivity.class);
			this.startActivity(intent1);
			break;
		default:
			return super.onOptionsItemSelected(item);
				
		}
		return true;
	}
	/**
	 * back button is disabled
	 */
	@Override
	public void onBackPressed() {

		// do nothing!

	}

}
